package com.StarStudio.Utils;

import com.ruoyi.common.core.Entity.UserInfo;

import java.io.File;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成用户工具类
 */
public class UserUtil {


    public static void createUser(int count) throws Exception {
        List<UserInfo> users = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            UserInfo user = new UserInfo();
            user.setId(18000000000L + i);
            user.setReal_name("user" + i);
            user.setBeans(1000);
            users.add(user);
        }
        //插入数据库
        Connection connect = getConnect();
        String sql = "insert into user_info(id,real_name,beans) values(?,?,?)";
        PreparedStatement preparedStatement = connect.prepareStatement(sql);
        for (int i = 0; i < users.size(); i++) {
            UserInfo user = users.get(i);
            preparedStatement.setLong(1,user.getId());
            preparedStatement.setString(2,user.getReal_name());
            preparedStatement.setInt(3, user.getBeans());
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
        preparedStatement.clearParameters();
        connect.close();
        System.out.println("insert into database");


        File file = new File("C:\\Users\\92149\\Desktop\\user.txt");
        if (file.exists()){
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        raf.seek(0);
        for (UserInfo user : users) {
            String row = user.getId() + "," + "29";
            raf.seek(raf.length());
            raf.write(row.getBytes());
            raf.write("\r\n".getBytes());
        }
        raf.close();
        System.out.println("over");
    }

    private static Connection getConnect() throws Exception{
        String url = "jdbc:mysql://localhost:3306/ybmall?userUnicode=ture&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "971014";
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    }


    public static void main(String[] args) throws Exception {
        createUser(5000);
    }
}
